home *** CD-ROM | disk | FTP | other *** search
- #include "MyHeader.h"
-
- static MyMatrix unit = {
- 1, 0, 0,
- 0, 1, 0,
- 0, 0, 1,
- 0, 0, 0
- };
-
- void MyMatrixClear(MyMatrix * m1)
- {
- *m1 = unit;
- }
-
- void MyMatrixCat(MyMatrix * m1, MyMatrix * m2, MyMatrix * m3)
- {
- MyMatrix mt;
-
- MyMatrixRotateVector(m2, &m1->x, &mt.x);
- MyMatrixRotateVector(m2, &m1->y, &mt.y);
- MyMatrixRotateVector(m2, &m1->z, &mt.z);
- MyMatrixTransformVector(m2, &m1->w, &mt.w);
-
- *m3 = mt;
- }
-
- void MyMatrixRotateByMatrix(MyMatrix * m1, MyMatrix * m2)
- {
- MyVector temp;
- static const MyVector zeroVector = {0, 0, 0};
-
- temp = m1->w;
-
- m1->w = zeroVector;
-
- MyMatrixCat(m1, m2, m1);
-
- m1->w = temp;
- }
-
- void MyMatrixTransformVector(MyMatrix * m1, MyVector * v1, MyVector * v2)
- {
- MyVector vt;
-
- vt.x = m1->x.x * v1->x + m1->y.x * v1->y + m1->z.x * v1->z + m1->w.x;
- vt.y = m1->x.y * v1->x + m1->y.y * v1->y + m1->z.y * v1->z + m1->w.y;
- vt.z = m1->x.z * v1->x + m1->y.z * v1->y + m1->z.z * v1->z + m1->w.z;
-
- *v2 = vt;
- }
-
- // does not translate the vector by the w value
- void MyMatrixRotateVector(MyMatrix * m1, MyVector * v1, MyVector * v2)
- {
- MyVector vt;
-
- vt.x = m1->x.x * v1->x + m1->y.x * v1->y + m1->z.x * v1->z;
- vt.y = m1->x.y * v1->x + m1->y.y * v1->y + m1->z.y * v1->z;
- vt.z = m1->x.z * v1->x + m1->y.z * v1->y + m1->z.z * v1->z;
-
- *v2 = vt;
- }
-
- void MyMatrixSetRotateX(float theta, MyMatrix * m1)
- {
- *m1 = unit;
- m1->y.y = m1->z.z = cos(theta);
- m1->z.y = -(m1->y.z = sin(theta));
- }
-
- void MyMatrixSetRotateY(float theta, MyMatrix * m1)
- {
- *m1 = unit;
- m1->x.x = m1->z.z = cos(theta);
- m1->x.z = -(m1->z.x = sin(theta));
- }
-
- void MyMatrixSetRotateZ(float theta, MyMatrix * m1)
- {
- *m1 = unit;
- m1->y.y = m1->x.x = cos(theta);
- m1->y.x = -(m1->x.y = sin(theta));
- }
-
- void MyMatrixNegate(MyMatrix * m1, MyMatrix * m2)
- {
- m2->w.x = m1->x.x * -m1->w.x + m1->x.y * -m1->w.y + m1->x.z * -m1->w.z;
- m2->w.y = m1->y.x * -m1->w.x + m1->y.y * -m1->w.y + m1->y.z * -m1->w.z;
- m2->w.z = m1->z.x * -m1->w.x + m1->z.y * -m1->w.y + m1->z.z * -m1->w.z;
-
- m2->x.x = m1->x.x;
- m2->x.y = m1->y.x;
- m2->x.z = m1->z.x;
-
- m2->y.x = m1->x.y;
- m2->y.y = m1->y.y;
- m2->y.z = m1->z.y;
-
- m2->z.x = m1->x.z;
- m2->z.y = m1->y.z;
- m2->z.z = m1->z.z;
-
- }
-
- void MyMatrixScaleLocal(MyMatrix * m1, float scaleFactor, MyMatrix * m2)
- {
- m2->x.x = m1->x.x * scaleFactor;
- m2->x.y = m1->x.y * scaleFactor;
- m2->x.z = m1->x.z * scaleFactor;
-
- m2->y.x = m1->y.x * scaleFactor;
- m2->y.y = m1->y.y * scaleFactor;
- m2->y.z = m1->y.z * scaleFactor;
-
- m2->z.x = m1->z.x * scaleFactor;
- m2->z.y = m1->z.y * scaleFactor;
- m2->z.z = m1->z.z * scaleFactor;
-
- }
-
-
-
- /**************************************/
-
- static void MyMatrixPrint(MyMatrix * m1)
- {
- printf("\n%f, %f, %f\n", m1->x.x, m1->x.y, m1->x.z);
- printf("%f, %f, %f\n", m1->y.x, m1->y.y, m1->y.z);
- printf("%f, %f, %f\n", m1->z.x, m1->z.y, m1->z.z);
- printf("%f, %f, %f\n", m1->w.x, m1->w.y, m1->w.z);
- }
-
- void mtest(void)
- {
- MyMatrix m1, m2;
- char s[256];
- float xr, yr;
-
- double pi;
-
- /*****/
-
- pi = atan(1) * 4;
-
- printf("pi = %20.15f\n", pi);
-
- printf("enter x rotation");
- scanf("%f", &xr);
- printf("\nenter y rotation");
- scanf("%f", &yr);
- printf("\n");
-
- MyMatrixSetRotateX(xr, &m1);
- MyMatrixPrint(&m1);
-
- MyMatrixSetRotateY(yr, &m2);
- MyMatrixPrint(&m2);
-
- MyMatrixCat(&m1, &m2, &m1);
- MyMatrixPrint(&m1);
-
- gets(s);
-
- }
-
-
-
-
-
-